package de.test.NeutrinoAndroidRemote.UI;

import java.util.Map;

import de.test.NeutrinoAndroidRemote.R;
import de.test.NeutrinoAndroidRemote.Tools.Downloader;

import de.test.NeutrinoAndroidRemote.Tools.Parser;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SwitchChannel extends Activity {
	Map<String, Integer> myBouquets;
	Map<String, String> myChannels;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.switchchannel);
        

        //final TextView txtResult = (TextView)findViewById(R.id.textView1);
        
        final ListView lv = (ListView)findViewById(R.id.listView1);
        //TODO: globaler Zugriff auf Prefs_name
		SharedPreferences settings = getSharedPreferences("NeutrinoAndroidRemotePrefs", MODE_PRIVATE);
		String ip = settings.getString("ipAddress", ""); 
        String result = Downloader.Get("http://"+ip+"/control/getbouquets");
        myBouquets = Parser.GetBouquets(result);
        
        String lv_arr[]=new String[myBouquets.size()];
        int i=0;
        
        for ( String elem : myBouquets.keySet() ){
        		 lv_arr[i] = elem;
        		 i++;
        }
        		
		lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));
		 lv.setOnItemClickListener(listenerOnBouquetClick);



    }
    
    private OnItemClickListener listenerOnBouquetClick = new OnItemClickListener() {
    	
		public void onItemClick(AdapterView<?> arg0, View item,
				int arg2, long arg3) {
			
			Integer id = myBouquets.get(((TextView)item).getText());
	        String result = Downloader.Get("http://192.168.2.105/control/getbouquet?bouquet="+id);
	        
	        final ListView lv = (ListView)findViewById(R.id.listView1);
	        myChannels = Parser.GetChannels(result);
	        
	        String lv_arr[]=new String[myChannels.size()];
	        int i=0;
	        
	        for ( String elem : myChannels.keySet() ){
	        		 lv_arr[i] = elem;
	        		 i++;
	        }
	        		
			lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, lv_arr));
			lv.setOnItemClickListener(listenerOnChannelClick);
		}
    };
    
    private OnItemClickListener listenerOnChannelClick = new OnItemClickListener() {
    	
		public void onItemClick(AdapterView<?> arg0, View item,
				int arg2, long arg3) {
			
			String id = myChannels.get(((TextView)item).getText());
			//TODO: globaler Zugriff auf Prefs_name
			SharedPreferences settings = getSharedPreferences("NeutrinoAndroidRemotePrefs", MODE_PRIVATE);
			String ip = settings.getString("ipAddress", ""); 
	        String result = Downloader.Get("http://"+ip+"/control/zapto?"+id);
	        
        	Toast.makeText(getApplicationContext(), result,
        	          Toast.LENGTH_SHORT).show();
	       finish(); //alles erledigt.. oder auch nicht

		}
    };

}
